from flask import Flask,request,session,render_template
from application.admin import admin
from application.db_util import db
from db_util.models import Admin,User
from sqlalchemy import or_
#######################################
#         定义接口：管理员模块
#######################################

#跳转到管理员登陆界面
@admin.route("/admin/to_admin_login")
def to_admin_login():
    return render_template("admin/login.html");

#管理员登录
@admin.route("/admin/admin_login",methods=['GET','POST'])
def admin_login():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        user_judge = db.session.query(Admin).filter(Admin.username==username).all()
        if user_judge:
            if user_judge[0].password == password:
                session["login"]=1
                session["username"]=user_judge[0].username
                session["user_id"]=user_judge[0].id
                #获取用户列表
                user_info_list = db.session.query(User).all();
                db.session.close();
                return render_template("admin/admin_home.html",user_info_list = user_info_list);
            else:
                db.session.close();
                return render_template("admin/login.html",login_res="用户名或密码错误！")
        else:
            db.session.close();
            return render_template("admin/login.html",login_res="用户名不存在！");
    else:
        db.session.close();
        return render_template("admin/login.html",login_res="请求错误！");

#管理员修改密码
@admin.route("/admin/admin_update",methods=['GET','POST'])
def admin_update():
    admin_pwd = request.form.get('password')
    admin = Admin()
    admin.password = admin_pwd;
    admin.id = session["user_id"]
    if admin.id==0:
        db.session.close();
        return("用户尚未登陆")
    else:
        db.session.close();
        admin.username = session["username"]
        db.session.merge(admin);
        db.session.commit()
        return("修改完成")

#管理员注销
@admin.route("/admin/admin_logout",methods=['GET','POST'])
def admin_logout():
    session.clear()
    session["login"] = 0
    session["user_id"] = 0
    return render_template("user/login.html")

#使用人数统计
@admin.route("/admin/user_count",methods=['GET','POST'])
def user_count():
    user_judge = db.session.query(User).all()
    if user_judge:
        user_nums= len(user_judge)
        # user_nums = str(user_nums)
        db.session.close();
        return("当前用户："+str(user_nums))
    else:
        db.session.close();
        return("尚无用户")

#用户查找
@admin.route("/admin/user_select")
def user_select():
    user_name = request.args.get("user_name");
    colleague_list = db.session.query(User).filter(
        or_(User.username.like('%' + user_name + '%'), User.nickname.like('%' + user_name + '%'))).all();
    user_info_list = db.session.query(User).all();
    db.session.close();
    if (colleague_list):
        db.session.close();
        return render_template("admin/admin_home.html", user_info_list=user_info_list,
                               colleague_list=colleague_list)
    else:
        db.session.close();
        return ("ERROR")

#删除用户
@admin.route("/admin/user_delete")
def user_delete():
    user_id = request.args.get("user_id");
    user = db.session.query(User).filter(User.id == user_id).all();
    if(user):
        del_info = "删除成功！";
    else:
        del_info = "请勿重复提交！";
    db.session.execute('delete from user where id=:id', {'id': user_id})
    db.session.commit();
    # 获取用户列表
    user_info_list = db.session.query(User).all();
    db.session.close();
    return render_template("admin/admin_home.html",user_info_list = user_info_list,del_info=del_info);


#访问次数统计
# @admin.route("/admin/visit_times",methods=['GET','POST'])
# def visit_times():
#     return


